import Vue  from 'vue'
import Router from 'vue-router'



Vue.use(Router)

const Login = ()=> import("../components/login.vue")
const Home = () => import("../components/Home")
const Welcome = () => import("../components/homeComponents/Welcome")
const Users = ()=> import("../components/user/User")
const Rights = () => import("../components/rights/Rights")
const Roles = ()=> import("../components/rights/Roles")
const GoodsCate =() => import("../components/goods/GoodsCate")
const GoodsParams = ()=> import("../components/goods/GoodsParams")

const routes = [
    // 路由充重定向
    {
        path:'/',
        redirect:'/login',

    },
    {
        path:'/login',
        component:Login,
        beforeEnter: (to, from ,next)=>{
            if(!localStorage.getItem('token')){
                next()
            }else {
                router.push('/welcome')
            }
        }
    },
    {
        path:'/home',
        component:Home,
        redirect: '/welcome',
        children:[
            {
                path:'/welcome',
                component:Welcome
            },
            {
                path:'/users',
                component:Users
            },
            {
                path: '/rights',
                component:Rights
            },
            {
                path: '/roles',
                component:Roles
            },
            {
                path:'/categories',
                component:GoodsCate
            },
            {
                path:'/params',
                component:GoodsParams
            }

        ]
    }
]

const router =  new Router({
    routes,
    mode:'history'
})
// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}



// 设置路由导航守卫

router.beforeEach((to,from,next) =>{
    // console.log(to);
    // console.log(from);
    next()
    if(to.path !=='/login'){
        const token = localStorage.getItem('token')
        token ? next() : router.push('/login')
    }else {
        next()
    }

})

export  default router